﻿using Envision4IT.data;
using Envision4IT.data.model;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity.Migrations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Envision4IT.data
{
    public static class Seeder
    {
        private static string Admin;
        private static string User;
        private static string Tech;
        private static string Root;
        private static string User2;
        private static string User3;

        public static void Seed(ApplicationDbContext db,
            bool roles = true,
            bool users = true,
            bool userdata = true,
            bool project = true,
            bool hardware = true,
            bool ticket = true,
            bool faq = true,
            bool customercontact = true,
            bool solution = true,
            bool document = true,
            bool ticketnote = true,
            bool solutionnote = true,
            bool projectnote = true,
            bool customernote = true)
        {
            if (roles) seedroles(db);
            if (users) seedusers(db);
            User = db.Users.FirstOrDefault(x => x.UserName == "user@test.com").Id;
            Tech = db.Users.FirstOrDefault(x => x.UserName == "tech@test.com").Id;
            Admin = db.Users.FirstOrDefault(x => x.UserName == "admin@test.com").Id;
            Root = db.Users.FirstOrDefault(x => x.UserName == "root@test.com").Id;
            User2 = db.Users.FirstOrDefault(x => x.UserName == "user2@test.com").Id;
            User3 = db.Users.FirstOrDefault(x => x.UserName == "user3@test.com").Id;

            if (project) seedprojects(db);
            if (hardware) seedhardware(db);
            if (ticket) seedtickets(db);
            if (faq) seedfaq(db);
            if (customernote) seedCustomerNote(db);
            if (solution) seedsolutions(db);
            if (document) seeddocument(db);
            if (ticketnote) seedTicketNotes(db);
            if (solutionnote) seedSolutionNote(db);
            if (projectnote) seedProjectNotes(db);
            if (customernote) seedCustomerNote(db);
        }

        private static void seedroles(ApplicationDbContext db)
        {
            var store = new RoleStore<IdentityRole>(db);
            var manager = new RoleManager<IdentityRole>(store);
            {
                if (!manager.RoleExists(Roles.ADMIN))
                {
                    manager.Create(new IdentityRole() { Name = Roles.ADMIN });
                }
                if (!manager.RoleExists(Roles.CUSTOMER))
                {
                    manager.Create(new IdentityRole() { Name = Roles.CUSTOMER });
                }
                if (!manager.RoleExists(Roles.ROOT))
                {
                    manager.Create(new IdentityRole() { Name = Roles.ROOT });
                }
                if (!manager.RoleExists(Roles.TECH))
                {
                    manager.Create(new IdentityRole() { Name = Roles.TECH });
                }
            }
        }

        private static void seedusers(ApplicationDbContext db)
        {
            var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));

            if (!db.Users.Any(x => x.UserName == "user@test.com"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    Email = "user@test.com",
                    UserName = "user@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, Roles.CUSTOMER);
            }

            if (!db.Users.Any(x => x.UserName == "tech@test.com"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    Email = "tech@test.com",
                    UserName = "tech@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, Roles.TECH);
            }
            if (!db.Users.Any(x => x.UserName == "admin@test.com"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    Email = "admin@test.com",
                    UserName = "admin@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, Roles.ADMIN);
            }
            if (!db.Users.Any(x => x.UserName == "root@test.com"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    Email = "root@test.com",
                    UserName = "root@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, Roles.ROOT);
            }

            if (!db.Users.Any(x => x.UserName == "user2@test.com"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    Email = "user2@test.com",
                    UserName = "user2@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, Roles.CUSTOMER);
            }
            if (!db.Users.Any(x => x.UserName == "user3@test.com"))
            {
                ApplicationUser user = new ApplicationUser()
                {
                    Email = "user3@test.com",
                    UserName = "user3@test.com"
                };
                manager.Create(user, "123123");
                manager.AddToRole(user.Id, Roles.CUSTOMER);
            }
        }

        private static void seedprojects(ApplicationDbContext db)
        {
            db.Projects.AddOrUpdate(
                p => p.ProjectID,
            new Project { ProjectID = 1, UserID = "1", StartDate = Convert.ToDateTime("07/24/2015"), ProjectName = "Quoc's House", TargetDate = Convert.ToDateTime("12/31/2027") },
            new Project { ProjectID = 2, UserID = "5", StartDate = Convert.ToDateTime("07/24/2015"), ProjectName = "The White House", TargetDate = Convert.ToDateTime("03/20/2017") },
            new Project { ProjectID = 3, UserID = "6", StartDate = Convert.ToDateTime("08/30/2015"), ProjectName = "Pentagon Home Lobby", TargetDate = Convert.ToDateTime("01/01/2016") },
            new Project { ProjectID = 7, UserID = "1", StartDate = Convert.ToDateTime("07/01/2015"), ProjectName = "Luxury San Francisco Hotel", TargetDate = Convert.ToDateTime("01/31/2016") },
            new Project { ProjectID = 8, UserID = "5", StartDate = Convert.ToDateTime("08/01/2015"), ProjectName = "Gio's Home Theater", TargetDate = Convert.ToDateTime("08/31/2016") },
            new Project { ProjectID = 9, UserID = "6", StartDate = Convert.ToDateTime("09/10/2015"), ProjectName = "XYZ, Inc. Headquarters Building", TargetDate = Convert.ToDateTime("12/31/2015") },
            new Project { ProjectID = 4, UserID = "2", ProjectName = "Full upgrade", CompletionDate = Convert.ToDateTime("09/30/2015"), StartDate = Convert.ToDateTime("07/5/2015"), TargetDate = Convert.ToDateTime("08/19/2015") },
            new Project { ProjectID = 5, UserID = "2", ProjectName = "Instalation of Wifi", CompletionDate = Convert.ToDateTime("10/30/2016"), StartDate = Convert.ToDateTime("09/30/2016"), TargetDate = Convert.ToDateTime("10/19/2016") },
            new Project { ProjectID = 6, UserID = "2", ProjectName = "Smart Home", CompletionDate = Convert.ToDateTime("01/01/2016"), StartDate = Convert.ToDateTime("12/05/2015"), TargetDate = Convert.ToDateTime("01/01/2016") }
            );
        }

        private static void seedsolutions(ApplicationDbContext db)
        {
            db.Solutions.AddOrUpdate(
                s => s.SolutionID,
            new Solution { SolutionID = 1, ProjectID = 1, Urgency = "Kind Of", TargetCompletionDate = Convert.ToDateTime("12/31/2027"), StartDate = Convert.ToDateTime("07/24/2015") },
            new Solution { SolutionID = 2, ProjectID = 2, Urgency = "Not Really", TargetCompletionDate = Convert.ToDateTime("03/20/2017"), StartDate = Convert.ToDateTime("07/24/2015") },
            new Solution { SolutionID = 3, ProjectID = 3, Urgency = "Very", TargetCompletionDate = Convert.ToDateTime("01/01/2016"), StartDate = Convert.ToDateTime("08/27/2015") },
            new Solution { SolutionID = 4, StartDate = Convert.ToDateTime("07/01/2015"), ProjectID = 9, TargetCompletionDate = Convert.ToDateTime("07/15/2016"), Urgency = "one" },
            new Solution { SolutionID = 5, ProjectID = 4, Urgency = "Very urgent", TargetCompletionDate = Convert.ToDateTime("09/25/2015"), StartDate = Convert.ToDateTime("09/21/2015") },
            new Solution { SolutionID = 6, ProjectID = 5, Urgency = "Not so important", TargetCompletionDate = Convert.ToDateTime("10/25/2016"), StartDate = Convert.ToDateTime("09/21/2015") },
            new Solution { SolutionID = 7, ProjectID = 6, Urgency = "High priority", TargetCompletionDate = Convert.ToDateTime("12/25/2015"), StartDate = Convert.ToDateTime("09/21/2015") },
            new Solution { SolutionID = 8, StartDate = Convert.ToDateTime("07/01/2015"), ProjectID = 9, TargetCompletionDate = Convert.ToDateTime("07/15/2016"), Urgency = "one", },
            new Solution { SolutionID = 9, StartDate = Convert.ToDateTime("07/01/2015"), ProjectID = 7, TargetCompletionDate = Convert.ToDateTime("07/15/2016"), Urgency = "one", },
            new Solution { SolutionID = 10, StartDate = Convert.ToDateTime("07/01/2015"), ProjectID = 8, TargetCompletionDate = Convert.ToDateTime("07/15/2016"), Urgency = "one", }
            );
        }

        private static void seedhardware(ApplicationDbContext db)
        {
            db.Hardware.AddOrUpdate(
                p => p.HardwareID,
                new Hardware { HardwareID = 1, HardwareName = "ping-pong table" },
                new Hardware { HardwareID = 2, HardwareName = "Cisco Router x123" },
                new Hardware { HardwareID = 3, HardwareName = "solar panels" },
                new Hardware { Category = Category.Internet, HardwareID = 4, HardwareName = "Wifi cables", Notes = "Notes are for girls" },
                new Hardware { Category = Category.Sound, HardwareID = 5, HardwareName = "Wires for speakers", Notes = "Notes are for men" },
                new Hardware { Category = Category.Video, HardwareID = 6, HardwareName = "hardware for video ", Notes = "Notes are for everyone" },
                new Hardware { HardwareID = 7, HardwareName = "Samsung 70in OLED TV", Notes = "This TV has 5 HDMI inputs.", Category = Category.Video },
                new Hardware { HardwareID = 8, HardwareName = "Cisco Wi-Fi Router /a/b/g/n", Notes = "5 ethernet ports", Category = Category.Internet },
                new Hardware { HardwareID = 9, HardwareName = "Conference room Speakerphone", Notes = "RJ-45 or wireless", Category = Category.Telecommunications }

                );
        }

        private static void seedfaq(ApplicationDbContext db)
        {
            db.FAQS.AddOrUpdate(
                f => f.FaqID,
                new FAQ { FaqID = 1, HardwareID = 1, Answer = "don't forget to turn on the ping pongs", Question = "my table isn't working", Topic = "troubleshooting" },
                new FAQ { FaqID = 2, HardwareID = 2, Answer = "don't forget to turn on the router", Question = "my router isn't working", Topic = "troubleshooting" },
                new FAQ { FaqID = 3, HardwareID = 3, Answer = "don't forget to turn on the solar panels", Question = "my solar isn't working", Topic = "troubleshooting" },
                new FAQ { FaqID = 4, Answer = "The anser is found at the end of a bottle", HardwareID = 4, Question = "Why isnt this working?" },
                new FAQ { FaqID = 5, Answer = "Serch within yourself", HardwareID = 5, Question = "Why isnt this working properly?" },
                new FAQ { FaqID = 6, Answer = "Never forget where you came from", HardwareID = 6, Question = "Why isnt this working correctly?" }

                );
        }

        private static void seeddocument(ApplicationDbContext db)
        {
            db.Documents.AddOrUpdate(
                d => d.DocumentID,
                new Document { DocumentID = 1, HardwareID = 1, Filepath = "http://www.atmel.com/images/doc4316.pdf", Url = "http://ciarang.com/gallery/albums/general/technical/amigahwref.jpg" },
                 new Document { DocumentID = 2, HardwareID = 2, Filepath = "http://www.atmel.com/images/doc4316.pdf", Url = "http://ciarang.com/gallery/albums/general/technical/amigahwref.jpg" },
                  new Document { DocumentID = 3, HardwareID = 3, Filepath = "http://www.atmel.com/images/doc4316.pdf", Url = "http://ciarang.com/gallery/albums/general/technical/amigahwref.jpg" }
                );
        }

        private static void seedcustomercontact(ApplicationDbContext db)
        {
            db.CustomerContacts.AddOrUpdate(
                c => c.CustomerContactID,
                new CustomerContact { CustomerContactID = 1, Title = "Manager", Email = "fake@fake.com", Department = "housing", Name = "Jason", PhoneNumber = 1234567890 },
                new CustomerContact { CustomerContactID = 2, Title = "Owner", Email = "fake@fake.com", Department = "housing", Name = "Quinn", PhoneNumber = 1234567890 },
                new CustomerContact { CustomerContactID = 3, Title = "Little Brother", Email = "fake@fake.com", Department = "hhousing", Name = "Phil", PhoneNumber = 1234567890 },
                new CustomerContact { CustomerContactID = 4 },
                new CustomerContact { CustomerContactID = 5 },
                new CustomerContact { CustomerContactID = 6 }
                );
        }

        private static void seedtickets(ApplicationDbContext db)
        {
            db.Tickets.AddOrUpdate(
                t => t.TicketID,
                new Ticket { TicketID = 4, SolutionID = 5, DateOpened = Convert.ToDateTime("03/04/2015") },
                new Ticket { TicketID = 5, SolutionID = 4, DateOpened = Convert.ToDateTime("03/08/2014") },
                new Ticket { TicketID = 6, SolutionID = 6, DateOpened = Convert.ToDateTime("03/09/2016") }

                    );
        }

        private static void seedTicketNotes(ApplicationDbContext db)
        {
            db.TicketNotes.AddOrUpdate(
                v => v.TicketNoteID,
                new TicketNote { AdminID = "4", TicketNoteID = 5, CustomerID = "5", TicketID = 4, DateEntered = Convert.ToDateTime("04/05/2014"), Note = "This is a note for ra ticket note id number 5" },
                new TicketNote { AdminID = "5", TicketNoteID = 4, CustomerID = "4", TicketID = 5, DateEntered = Convert.ToDateTime("05/07/2014"), Note = "This is a note for ra ticket note id number 4" },
                new TicketNote { AdminID = "6", TicketNoteID = 6, CustomerID = "6", TicketID = 6, DateEntered = Convert.ToDateTime("04/05/2014"), Note = "This is a note for ra ticket note id number 6" }

                );
        }

        private static void seedSolutionNote(ApplicationDbContext db)
        {
            db.SolutionNotes.AddOrUpdate(
                x => x.SolutionNoteID,
                new SolutionNote { SolutionNoteID = 4, DateEntered = Convert.ToDateTime("04/05/2014"), Note = "This is a Solution note for SolutionNoteID 4", SolutionID = 4 },
                new SolutionNote { SolutionNoteID = 5, DateEntered = Convert.ToDateTime("04/05/2014"), Note = "This is a Solution note for SolutionNoteID 5", SolutionID = 5 },
                new SolutionNote { SolutionNoteID = 6, DateEntered = Convert.ToDateTime("04/05/2014"), Note = "This is a Solution note for SolutionNoteID 6", SolutionID = 6 }

                );
        }

        private static void seedProjectNotes(ApplicationDbContext db)
        {
            db.ProjectNotes.AddOrUpdate(
                n => n.ProjectNoteID,
                new ProjectNote { DateEntered = Convert.ToDateTime("04/05/2014"), ProjectNoteID = 4, Note = "this is note id 4", ProjectID = 4, UserID = "4" },
                new ProjectNote { DateEntered = Convert.ToDateTime("04/05/2014"), ProjectNoteID = 5, Note = "this is note id 5", ProjectID = 5, UserID = "5" },
                new ProjectNote { DateEntered = Convert.ToDateTime("04/14/2014"), ProjectNoteID = 6, Note = "this is note id 6", ProjectID = 6, UserID = "6" }
                );
        }

        private static void seedCustomerNote(ApplicationDbContext db)
        {
            db.CustomerNotes.AddOrUpdate(
                x => x.CustomerNoteID,
                new CustomerNote { CustomerNoteID = 4, AdminID = "6", DateEntered = Convert.ToDateTime("04/05/2014"), Note = "THis is a note for a cusNoteID 4" },
                new CustomerNote { CustomerNoteID = 5, CustomerID = "4", DateEntered = Convert.ToDateTime("04/05/2014"), Note = "THis is a note for a cusNoteID 5" },
                new CustomerNote { CustomerNoteID = 6, CustomerID = "5", DateEntered = Convert.ToDateTime("04/05/2014"), Note = "THis is a note for a cusNoteID 6" }

                );
        }
    }
}